<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Admin_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function group($key = null){
		$return = array(
			'59' => '学校管理员',
			'99' => '超级管理员'
		);
		if($key !== null){
			$return = $return[$key];
		}
		return $return;
	}
	
	public function login($data){
		if(!$data || !$data['username'] || !$data['password']){
			return false;
		}
		$this->load->helper('common');
		$user = $this->get_row([
			'username' => $data['username'],
			'password' => mmd5($data['password'])
		]);
		if(!$user){
			return false;
		}
		unset($user['password']);
		if(!$this->update_login($user)){
			return false;
		}
		return $user;
	}
	
	// 更新登录信息
	private function update_login(&$user){
		$uid = $user['id'];
		$data = array(
			'logintime' => time()
		);
		$query = $this->set($data, $uid);
		if($query){
			$user = array_merge($user, $data);
			return true;
		}
		return false;
	}
	
}